package pl.edu.pw.polygen.modeler.client.object.listener;

import pl.edu.pw.polygen.modeler.client.object.IPolygenObject;
import pl.edu.pw.polygen.modeler.client.workspace.IWorkspace;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseUpEvent;

public class MergeVertexListener extends BaseObjectListener {

	@Override
	public void onMouseDown(MouseDownEvent event, IPolygenObject polygenObject) {
		IWorkspace context = polygenObject.getContext();
		context.unselectAll();
		context.setMouseDownCatch(true);
		polygenObject.hold();
	}

	@Override
	public void onMouseUp(MouseUpEvent event, IPolygenObject polygenObject) {
		if(polygenObject.isHolded()==true){
			polygenObject.drop();
			polygenObject.select();
		}else{
			IPolygenObject sourceVertex = polygenObject.getContext().getHolded();
			polygenObject.merge(sourceVertex,polygenObject);
			sourceVertex.drop();
			sourceVertex.remove();
			polygenObject.select();
			
		}
	}

	@Override
	public void onMouseOver(MouseOverEvent event, IPolygenObject polygenObject) {
		if(polygenObject.isSelected()==false && polygenObject.isHolded()==false){
			polygenObject.over();
		}
	}

	@Override
	public void onMouseOut(MouseOutEvent event, IPolygenObject polygenObject) {
		if(polygenObject.isSelected()==false && polygenObject.isHolded()==false){
			polygenObject.out();
		}
	}


}
